package com.kpmg.datalake.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kpmg.datalake.common.dao.IndustryConfigMapper;
import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.enums.TopicEnum;
import com.kpmg.datalake.common.enums.UseOrDelEnum;
import com.kpmg.datalake.common.model.IndustryConfig;
import com.kpmg.datalake.common.service.IIndustryConfigService;
import com.kpmg.datalake.common.service.SqlParseService;
import com.kpmg.datalake.common.utils.AssertUtil;
import com.kpmg.datalake.common.vo.IndustryConfigVO;
import com.kpmg.datalake.common.vo.ServerResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 行业化配置 服务实现类
 * </p>
 *
 * @author eguo5
 * @since 2018-08-21
 */
@Service
public class IndustryConfigServiceImpl extends
    ServiceImpl<IndustryConfigMapper, IndustryConfig> implements IIndustryConfigService {

  private static final Logger logger = LoggerFactory.getLogger(IndustryConfigServiceImpl.class);
  @Resource
  private SqlParseService sqlParseService;

  /**
   * 获取行业信息
   */
  @Override
  public ServerResponse<ArrayList<Map<String, Object>>> getIndustry(
      IndustryConfigVO industryConfigVO) {
    try {
      List<Map<String, Object>> industryList = baseMapper
          .selectIndustryListByInfo(industryConfigVO.getIndustryInfo());
      return ServerResponse.createBySuccess("获取行业信息成功", new ArrayList<>(industryList));
    } catch (Exception e) {
      logger.error(e.getMessage());
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4015.getCode(),
          ResultCodeEnum.CODE4015.getDisplay());
    }

  }

  /**
   * 新增行业化配置记录
   */
  @Override
  public ServerResponse<String> insertIndustryConfigRecord(IndustryConfig industryConfig) {
    try {
      //判断行业化配置信息是否规范
      if (checkIndustryConfigInfo(industryConfig) != null) {
        return checkIndustryConfigInfo(industryConfig);
      }
      if (!super.insert(industryConfig)) {
        return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4021.getCode(),
            ResultCodeEnum.CODE4021.getDisplay());
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4015.getCode(),
          ResultCodeEnum.CODE4015.getDisplay());
    }
    return ServerResponse.createBySuccess();
  }

  /**
   * 获取行业化配置记录
   */
  @Override
  public ServerResponse<Page<IndustryConfig>> getIndustryConfigRecords(
      IndustryConfigVO industryConfigVO) {
    try {
      if (industryConfigVO.getIndustryConfig() == null) {
        industryConfigVO.setIndustryConfig(new IndustryConfig());
      }
      industryConfigVO.getIndustryConfig().setDelInd(UseOrDelEnum.USE.getIntCode());
      Page<IndustryConfig> page = industryConfigVO.getPage();
      page.setRecords(baseMapper.selectIndustryConfigRecords(industryConfigVO.getPage(),
          industryConfigVO.getIndustryConfig()));
      return ServerResponse.createBySuccess("获取行业化配置记录成功", page);
    } catch (Exception e) {
      logger.error(e.getMessage());
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4015.getCode(),
          ResultCodeEnum.CODE4015.getDisplay());
    }
  }

  /**
   * 修改行业化配置记录
   */
  @Override
  public ServerResponse<String> updateIndustryConfigRecord(IndustryConfig industryConfig) {
    try {
      //判断行业化配置信息是否规范
      if (checkIndustryConfigInfo(industryConfig) != null) {
        return checkIndustryConfigInfo(industryConfig);
      }
      industryConfig.setUdtDt(new Date());
      if (!super.updateAllColumnById(industryConfig)) {
        return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4021.getCode(),
            ResultCodeEnum.CODE4021.getDisplay());
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4015.getCode(),
          ResultCodeEnum.CODE4015.getDisplay());
    }
    return ServerResponse.createBySuccess();
  }

  /**
   * 判断行业化配置信息是否规范
   */
  public ServerResponse<String> checkIndustryConfigInfo(IndustryConfig industryConfig) {
    //判断行业或主题是否为空
    if (StringUtils.isEmpty(industryConfig.getIndustryId().trim()) || StringUtils
        .isEmpty(industryConfig.getTopic().trim())) {
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4027.getCode(),
          ResultCodeEnum.CODE4027.getDisplay());
    }
    //判断行业化建模脚本是否为空
    if (StringUtils.isEmpty(industryConfig.getIndustryModule().trim())) {
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4025.getCode(),
          ResultCodeEnum.CODE4025.getDisplay());
    }

    //判断行业化建模脚本sql语法
    if (!sqlParseService.parseStringToSql(industryConfig.getIndustryModule()).getKey()) {
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4028.getCode(),
          ResultCodeEnum.CODE4028.getDisplay());
    }
    //判断行业化数据处理脚本sql语法
    if (!StringUtils.isEmpty(industryConfig.getIndustryHanler().trim()) && !sqlParseService
        .parseStringToSql(industryConfig.getIndustryHanler()).getKey()) {
      return ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE4029.getCode(),
          ResultCodeEnum.CODE4029.getDisplay());
    }
    return null;
  }

  /**
   * 获取各类行业信息
   *
   * @param idyId 行业ID
   * @param topic 主题
   * @return 行业化配置信息
   */
  @Override
  public List<IndustryConfig> getListByIdyId(String idyId, TopicEnum topic) {
    AssertUtil.assertNotEmpty(idyId);
    IndustryConfig industryConfig = new IndustryConfig();
    industryConfig.setDelInd(UseOrDelEnum.USE.getIntCode());
    industryConfig.setIndustryId(idyId);
    EntityWrapper<IndustryConfig> industryConfigEntityWrapper = new EntityWrapper<>(industryConfig);
    // topic 中 initDb 和 initData 属于新增内部使用topic 用于执行相关脚本
    if (topic == null) {
      industryConfigEntityWrapper.notIn("topic",
          Arrays.asList(TopicEnum.INIT_DB.getCode(), TopicEnum.INIT_DATA.getCode()));
    } else if (TopicEnum.INIT_DB.getCode().equals(topic.getCode())) {
      industryConfig.setTopic(topic.getCode());
    } else if (TopicEnum.INIT_DATA.getCode().equals(topic.getCode())) {
      industryConfig.setTopic(topic.getCode());
    }
    industryConfigEntityWrapper.orderBy("industrySort", true);
    return super.selectList(industryConfigEntityWrapper);
  }
}
